﻿namespace Marmat.Forms
{
    public partial class CheckedComboBox
    {

        /// <summary>
        /// 获取指定的项。
        /// </summary>
        /// <param name="index"></param>
        /// <returns>列表项目来自于数据表时，可用此属性返回或设置所选择的行(DataRow类型)。
        /// 如果是自定义列表项目,此属性等同于SelectedValue。</returns>
        /// <remarks></remarks>
        public object GetItem(int index)
        {
            CheckedComboBoxForm PopupControl = (CheckedComboBoxForm)base.PopupControl;
            return PopupControl.CheckedListBoxControl1.GetItem(index);
        }

        /// <summary>
        /// 获取指定的项的值。
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public object GetItemValue(int index)
        {
            CheckedComboBoxForm PopupControl = (CheckedComboBoxForm)base.PopupControl;
            return PopupControl.CheckedListBoxControl1.GetItemValue(index);
        }


        /// <summary>
        /// 私有函数，用于更新Value属性，而不促发部分事件
        /// </summary>
        /// <remarks></remarks>
        protected internal void SetValue()
        {
            var Builder = new System.Text.StringBuilder();
            foreach (object item in this.SelectedItems)
            {
                if (Builder.Length > 0)
                    Builder.Append(this.SeparatorChar);
                Builder.Append(item.ToString());
            }

            this.Updating = true;
            this.Value = Builder.ToString();
            this.Updating = false;
        }

        public void ResetItems()
        {
            this.Items.Clear();
        }

        public bool ShouldSerializeItems()
        {
            return !(this.Items.Count == 0);
        }
    }
}